package com.zto.wk.webflux.kafka

/**
 * Created by wangkuan at 2019-02-22 11:02
 */
class MyUtil {
    fun print(string: String) {
        myUtil.print(string)
    }

    object myUtil {
        fun print(string: String) {
            println(string)
        }
    }

}

fun main(args: Array<String>) {
    lambdaDeconstruction()
}

val map = mapOf(1 to "to", 2 to "two")

//类型别名
typealias OscarWinners = Map<String, String>

fun countLaLaLand(oscarWinners: OscarWinners) = oscarWinners.count { it.value.contains("La La Land") }
fun checkLaLaLandIsTheBestMovies(oscarWinners: Map<String, String>) = oscarWinners["Best picture"] == "La La Land"

//已绑定的可调用引用
val numberRegex = "\\d+".toRegex()
val numbers = listOf("abc", "123", "456").filter(numberRegex::matches)

//密封类和数据类
sealed class Expr

data class Const(val number: Double) : Expr()
data class Sum(val e1: Expr, val e2: Expr) : Expr()
object NotANumber : Expr()

fun eval(expr: Expr): Double = when (expr) {
    is Const -> expr.number
    is Sum -> eval(expr.e1) + eval(expr.e2)
    NotANumber -> Double.NaN
}

val e = eval(Sum(Const(1.0), Const(2.0)))

//lambda 表达式中的解构
fun lambdaDeconstruction() {
    println(map.mapValues {
        val (k, v) = it
        "$k -> $v"
    })
    map.forEach { k, v -> println("$k -> $v") }
}

//下划线用于未使用的参数
fun useableParams() {
    map.forEach { _, value -> println(value) }
}

